package com.yipao.db.service.sales;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.yipao.db.dao.order.OrderMapper;
import com.yipao.db.domain.Order;
import com.yipao.db.domain.order.YipaoOrderExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.yipao.db.dao.sales.SalesMapper;
import com.yipao.db.domain.sales.Sales;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author voyboy
 */

@Service
public class AdminSalesService {
    @Resource
    private SalesMapper salesMapper;
    @Resource
    private OrderMapper orderMapper;



    public List<Order>selectByExample(String userId, Integer orderId, LocalDateTime start, LocalDateTime end, Integer page, Integer limit, String sort, String order){
        YipaoOrderExample example=new YipaoOrderExample();
        YipaoOrderExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(userId)){
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (orderId!=null ) {
            criteria.andIdEqualTo(Integer.valueOf(orderId));
        }
        if (start!=null) {
            criteria.andAddTimeGreaterThan(start);
        }
        if (end!=null) {
            criteria.andEndTimeLessThan(end);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page,limit);
        List<Order> orders = orderMapper.selectByExample(example);
        return orders;
    };


    public void add(Sales sales){
        sales.setAddTime(LocalDateTime.now());
        salesMapper.insert(sales);
    }


    public void update(Sales sales) {
        salesMapper.updateByPrimaryKeySelective(sales);
    }

    public void delete(Integer id){
        salesMapper.deleteByPrimaryKey(id);
    }
}
